<template>
  <el-row
    :gutter="40"
    style="margin:0px;"
  >
    <el-col :lg="20">
      <el-form
        :inline="true"
        :model="queryParams"
        label-position="right"
        size="small"
        class="filter-form-inline"
      >
        <el-row
          :gutter="40"
          style="margin-left: 0px; margin-right: 0px"
        >
          <el-col :span="6">
            <el-form-item
              label="状态"
              style="width: 100%"
            >
              <el-select
                v-model="queryParams.monthlyCheckStatus"
                style="width: 100%"
                placeholder="状态"
                @change="handlChange"
              >
                <el-option
                  label="全部状态"
                  value="distribute"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="盘点时间"
              style="width: 100%"
            >
              <date-picker
                v-model:date="queryParams.checkTime"
                type="month"
                placeholder="选择月"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="关键字"
              style="width: 100%"
            >
              <el-input
                v-model="queryParams.keyWord"
                clearable
                placeholder="关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="width: 100%">
              <el-button
                class="border-btn"
                @click="handlReset"
              >重置</el-button>
              <el-button
                type="primary"
                class="main-btn"
                @click="handlSearch"
              >搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-col>
  </el-row>

  <table-view
    header-cell-class-name="table-header"
    :data="tableData"
    border
    :cell-style="cellStyle"
    @row-click="aloneClick"
    @sort-change="handlSort"
  >
    <el-table-column
      label="盘点编号"
      align="center"
      type="index"
      width="80"
    />

    <el-table-column
      label="配送商名称"
      prop="distribute.distributeName"
      header-align="center"
      :min-width="200"
      :show-overflow-tooltip="true"
    />

    <el-table-column
      label="盘点时间"
      prop="checkTime"
      align="center"
      :show-overflow-tooltip="true"
    />

    <el-table-column
      label="创建人"
      prop="creatorName"
      align="center"
      :show-overflow-tooltip="true"
    />

    <el-table-column
      label="创建时间"
      align="center"
      :show-overflow-tooltip="true"
    >
      <template #default="{row}">
        {{ dateFormat("yyyy-mm-dd HH:MM:SS",row.createdAt) }}
      </template>
    </el-table-column>

    <el-table-column
      label="状态"
      align="center"
      :show-overflow-tooltip="true"
    >
      <template #default="{row}">
        <!-- 月度盘点状态（-2已撤销）（-1审核不通过）（0待审批） 1（有效） -->
        <span v-if="row.monthlyCheckStatus === '-2'">已撤销</span>
        <span v-if="row.monthlyCheckStatus === '-1'">审核不通过</span>
        <span v-if="row.monthlyCheckStatus === '0'">待审批</span>
        <span v-if="row.monthlyCheckStatus === '1'">有效</span>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template #default="{row}">
        <template v-if="row.monthlyCheckStatus === '-1' || row.monthlyCheckStatus === '-2'">
          <el-button
            type="text"
            size="mini"
            @click="handlDele(row.id)"
          >删除</el-button>
        </template>

      </template>

    </el-table-column>

  </table-view>
  <Pagination
    v-model:page="queryParams.page"
    v-model:limit="queryParams.pageSize"
    style="display: flex; justify-content: flex-end"
    :total="total"
    @pagination="handlList"
  />

  <ruleForm
    ref="ruleFormRef"
    @succsess="succsess"
  />
</template>

<script>
import {defineAsyncComponent, getCurrentInstance, inject, onMounted, reactive, toRefs,} from 'vue'
import {useRouter} from 'vue-router'

export default {
  name: 'Inventory',
  components: {
    ruleForm: defineAsyncComponent(() => import('./ruleForm.vue')),
  },
  setup() {
    const api = inject('api')
    const router = useRouter()
    const currentInstance = getCurrentInstance()
    const data = reactive({
      queryParams: {
        pageSize: 20,
        page: 1,
        keyWord: '',
        checkTime: '',
        monthlyCheckStatus: '',
      },
      total: 0,
      ruleFormRef: null,
      tableData: [{ a: '2' }],
    })

    const handlList = () => {
      api.inventory.INVENTORY_LIST(data.queryParams).then((res) => {
        if (res.code === 0) {
          data.tableData = res.data.list || []
          data.total = res.data.total
        }
      })
    }

    const handlReset = () => {
      data.queryParams = {
        pageSize: 20,
        page: 1,
        keyWords: '',
        date: null,
      }
      handlList()
    }

    // 单机表格
    const aloneClick = (row, column, event) => {
      if (column.label === '操作') return
      router.push({
        path: '/inventory/distributorInventory/inventoryDetail',
        query: { id: row.id },
      })
    }

    const cellStyle = () => {
      return 'cursor:pointer;'
    }

    const handlSearch = () => {
      data.queryParams.page = 1
      handlList()
    }

    const handlDele = (id) => {
      currentInstance.proxy
        .alertBox({
          message: '删除数据后不可恢复,确定删除？',
        })
        .then(() => {
          api.inventory.INVENTORY_DETELETE(id).then((res) => {
            if (res.code === 0) {
              handlList()
            } else {
              currentInstance.proxy.customMessage({
                type: 'error',
                message: res.msg,
              })
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    }

    const handlNew = () => {
      data.ruleFormRef.open()
    }

    const succsess = () => {
      handlList()
    }

    onMounted(() => {
      handlList()
    })

    return {
      ...toRefs(data),
      handlList,
      aloneClick,
      cellStyle,
      handlReset,
      handlSearch,
      handlDele,
      handlNew,
      succsess,
    }
  },
}
</script>

<style scoped>
/* @import url(); 引入css类 */
</style>
